package com.vrv.controller.collect;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.vrv.utils.Constans;
import com.vrv.utils.HttpClientUtil;
import com.vrv.utils.JsonUtil;

@Controller
@RequestMapping("/index/Index")
public class CollectHostController {

    @RequestMapping("/index")
    public String index(ModelMap model) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("target", "168.168.0.23");
        map.put("option", "getallnodes");
        map.put("services", "all");

        System.out.println(JsonUtil.MapToJson(map));
        String jsonStr = HttpClientUtil.getInstance().post(Constans.LOCALBEAT_URL,
                JsonUtil.MapToJson(map));

        JSONObject resultJson = JSONObject.parseObject(jsonStr);

        if (resultJson.containsKey("result")) {
            JSONObject resultJson2 = JSONObject.parseObject(resultJson.get("result").toString());
            List<String> ipList = JsonUtil.jsonToList(resultJson2.getString("nodes").toString());
            model.put("ipList", ipList);
        }

        return "/collect/collect_index";
    }

    @RequestMapping(value = "/toAddNodes")
    public String toAddNodes() {
        return "/collectNodes/add_nodes";
    }

    @RequestMapping(value = "/delNodes", produces = Constans.PRODUCES)
    @ResponseBody
    public String delNodes(String target) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("target", target);
        map.put("option", "delnode");
        map.put("beats", "all");
        map.put("object", "all");

        String jsonStr = HttpClientUtil.getInstance().post(Constans.LOCALBEAT_URL,
                JsonUtil.MapToJson(map));
        return jsonStr;
    }

    @RequestMapping(value = "/addNodes", produces = Constans.PRODUCES)
    @ResponseBody
    public String addNodes(String ip, String system) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("target", ip);
        map.put("option", "get");
        map.put("services", new String[] {
            "all"
        });
        map.put("object", "parameters");
        String resultJson = HttpClientUtil.getInstance().post(Constans.LOCALBEAT_URL,
                JsonUtil.MapToJson(map));
        return resultJson;
    }
}
